package com.xs.demo.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xs.demo.dao.UserDao;
import com.xs.demo.entity.Userinfo;
import com.xs.demo.service.ExportExcel;
import com.xs.demo.service.UserService;
/**
 * 
 * 类名称:ExportController 
 * @author 宗潇帅
 * 2014-11-18
 */
@Controller
@RequestMapping(value="/export")
public class ExportController {
	private static Log log = LogFactory.getLog(ExportController.class);
	UserService userService;
	UserDao userDao;
	ExportExcel<T>  excelService = new ExportExcel<T>();
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(params="method=sheetsExport")
	public void exportSheets(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		Userinfo userinfo = new Userinfo();
		List list = userService.lists(userinfo);//查询所有的数据
//		List list =  userService.listAllVO(); //根据字段查询
		String haders[] = new String []{"序号","名称","年龄","生日","地址","学号"};//查询所有的数据
//		String haders[] = new String []{"name","age","address"};//根据字段查询
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String filename = format.format(new Date().getTime())+".xls";
		response.setContentType("application/ms-excel;charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename="
				.concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
		OutputStream out = response.getOutputStream();
		try {
			excelService.exportExcel(haders,list,out);
			System.out.println("success");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}finally{
			out.close();
		}
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public ExportExcel<T> getExcelService() {
		return excelService;
	}
	public void setExcelService(ExportExcel<T> excelService) {
		this.excelService = excelService;
	}


	
}
